package org.hako.recite.word.tool;

import java.util.HashMap;
import java.util.Map;

import org.hako.recite.word.model.Word;
import org.hako.recite.word.parser.WordList;
import org.hako.recite.word.parser.WordListParser;
import org.hako.recite.word.parser.factory.WordListParserFactory;

/**
 * Analyze kannji frequency.
 * 
 * @since 0.4.14
 * @author <a href="mailto:xnnyygn@gmail.com">xnnyygn</a>
 */
public class AnalyzeKannjiFrequency {

	/**
	 * Main.
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		if (args.length < 1) {
			System.out.println("usage <filePath>");
			return;
		}
		WordListParserFactory parserFactory = new WordListParserFactory();
		String filePath = args[0];
		WordListParser parser = parserFactory.getParser(filePath);
		WordList wordList = parser.parse(filePath);
		Map<Character, Integer> kannjiMap = new HashMap<Character, Integer>();
		HiraganaTable hiraganaTable = new HiraganaTable();
		for (Word word : wordList) {
			char[] rawKannjiChars = word.get(0).toCharArray();
			int length = rawKannjiChars.length;
			for (int i = 0; i < length; i++) {
				Character c = new Character(rawKannjiChars[i]);
				if (!hiraganaTable.contains(c)) {
					if (kannjiMap.containsKey(c)) {
						kannjiMap.put(c, Integer.valueOf(kannjiMap.get(c)
								.intValue() + 1));
					} else {
						kannjiMap.put(c, Integer.valueOf(1));
					}
				}
			}
		}
		for (Map.Entry<Character, Integer> entry : kannjiMap.entrySet()) {
			System.out.println(entry.getKey() + "\t" + entry.getValue());
		}
	}

}
